layui.use(['form', 'layer', 'table'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        table = layui.table;

    //文章列表
    var tableIns = table.render({
        elem: '#tagList',
        url: '/manage/tag/data',
        cellMinWidth: 95,
        page: true,
        height: "full-125",
        limit: 5,
        limits: [10, 15, 20, 25],
        id: "tagListTable",
        cols: [[
            {field: 'id', title: '编号', align: "center"},
            {field: 'name', title: '标签名称', align: "center"},
            {field: 'count', title: '数量', align: "center"},
            {title: '操作', templet: '#tagListBar', align: "center"}
        ]]
    });

    
    //添加标签
    function add() {
        var index = layui.layer.open({
            title: "添加标签",
            type: 2,
            shadeClose: false,           //弹出框之外的地方是否可以点击
            area: ["320px", "180px"],
            content: "/manage/tag/add",
            success: function (layero, index) {
                var body = layui.layer.getChildFrame('body', index);
            }
        })
    }


    $(".add_btn").click(function () {
        add();
    })

    //列表操作
    table.on('tool(tagList)', function (obj) {
        var layEvent = obj.event,
            data = obj.data;

        if (layEvent === 'del') {
            //删除

            layer.confirm('删除标签将级联删除文章中的引入，是否继续？', {icon: 3, title: '提示信息'}, function (index) {
                $.post("/manage/tag/delete", {
                    id: data.id  //将需要删除的newsId作为参数传入
                }, function (data) {
                    data = JSON.parse(data)
                    if (data.flag === "success") {
                        layer.msg("删除成功" , {icon: 1});
                        tableIns.reload();
                        layer.close(index);
                    } else {
                        layer.msg("删除失败！", {icon: 2});
                    }
                })
            });

        }
    });

    //关闭
    $(".close").on("click", function () {
        parent.layer.close(parent.layer.getFrameIndex(window.name));
    });


})